#include <iostream>
#include <utility>
using namespace std;

int m = 0;
int Month[][2] = {{31, 31},
                {28, 29},
                {31, 31},
                {30, 30},
                {31, 31},
                {30, 30},
                {31, 31},
                {31, 31},
                {30, 30},
                {31, 31},
                {30, 30},
                {31, 31}};
int isLeap(int year)
{
    return ((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0)) ? 1 : 0;
}

int year, month, day, add, sub;

int main()
{
    cin >> m;
    while (m--)
    {
        scanf("%d %d %d %d", &year, &month, &day, &add);
        for (int i = 0; i < add; i++)
        {
            day++;
            if (day > Month[month - 1][isLeap(year)])
            {
                day = 1;
                month++;
                if (month > 12)
                {
                    year++;
                    month = 1;
                }
            }
        }
        printf("%04d-%02d-%02d\n", year, month, day);
    }
    return 0;
}